<template>
  <!-- 托管发布 -->
  <div class="change-auth">
    <div class="cell-wrapper">
      <div class="cell">
        <div class="label">智能托管发布</div>
        <div class="right">
          <span class="gray-text">已{{ isOpen ? '开启' : '关闭'}}</span>
          <div 
            :class="{'switch-active': isOpen}"
            class="my-switch" 
            @click="toggleSwitch">
            <span class="switch-circle"></span>
          </div>
        </div>
      </div>
      <div 
        class="material-list"
        v-if="isOpen"
      >
        <div class="tip">注：若当天有多条视频需要发布，建议设计多个发布时间，视频按照发布时间顺序依次进行发布</div>
        <div 
          :class="{'last-item': index === list.length - 1}"
          class="item" v-for="(item, index) in list" :key="index">
          <div class="item-left">
            <img src="@/assets/image/publish-time.png" alt="" class="time-icon"/>
            <span>发布时间{{ index + 1}}</span>
            <p class="time" @click="toggleTimeDialog(item.time)">{{ item.time }}</p>
          </div>
          <p class="item-right" @click="del(index)">删除</p>
        </div>
        <div class="add-material" @click="addTime">+添加新的发布时间</div>
      </div>
    </div>

    <van-popup v-model="showDialog"  position="bottom">
      <van-datetime-picker
        v-model="currentTime"
        type="time"
        title="选择时间"
        @confirm="timeConfirm"
        @cancel="timeCancel"
        :filter="filter"
      />
    </van-popup>
  </div>
</template>

<script>
import { updatePublishAuto, findPublishAuto } from '@/api/account'
import { Toast } from 'vant'
export default {
  data () {
    return {
      currentTime: '',
      showDialog: false,
      isOpen: true, // 是否打开
      list: [],
    }
  },
  mounted () {
    this.search()
    document.title = '托管发布'
  },
  methods: {
    filter(type, options) {
      if (type === 'minute') {
        return options.filter((option) => option % 30 === 0);
      }
      return options;
    },
    getFormList () {
      let arr = []
      this.list.forEach(item => {
        let obj = {}
        obj.hour = item.time.split(':')[0]
        obj.minute = item.time.split(':')[1]
        arr.push(obj)
      })
      return arr
    },
    // 查询托管发布信息
    search () {
      findPublishAuto().then(res => {
        console.log('查询发布信息：', res)
        this.isOpen = res.publish_auto !== 'OFF'
        if (this.isOpen !== 'OFF') {
          res.publish_time.forEach(item => {
            let h = item.hour < 10 ? '0' + item.hour : item.hour
            let m = item.minute < 10 ? '0' + item.minute : item.minute
            item.time = `${h}:${m}`
          })
          this.list = res.publish_time
        }
      })
    },
    updateInfo () {
      updatePublishAuto({
        publish_auto: this.isOpen ? 'ON' : 'OFF',
        publish_time: this.getFormList()
      }).then(res => {
        console.log('更新时间完成')
        this.showDialog = false
      })
    },
    del (index) {
      this.list.splice(index, 1)
      this.updateInfo()
    },
    toggleTimeDialog (time) {
      this.currentTime = time
      this.showDialog = true
    },
    timeCancel () {
      this.showDialog = false
    },
    timeConfirm (time) {
      if (this.list.findIndex(item => item.time === time) < 0) {
        let timeVal = time.split(':')[0] * 60 + time.split(':')[1] * 1
        this.list.push({ time, timeVal })
      }
      this.sortTime()
      this.updateInfo()
    },
    sortTime () {
      this.list.sort((a, b) => a.timeVal - b.timeVal)
    },
    toggleSwitch () {
      this.isOpen = !this.isOpen
      updatePublishAuto({
        publish_auto: this.isOpen ? 'ON' : 'OFF',
        publish_time: this.getFormList()
      }).then(res => {
        // Toast('关闭成功')
      })
    },
    addTime () {
      this.showDialog = true
    },
    remove (index) {
      console.log('remove index:', index)
    },
    toggleDialog () {
      this.showDialog = true
    }
  }
}
</script>

<style scoped>
.item-left {
  display: flex;
  align-items: center;
}
.time {
  margin-left: 2.4vw;
  width: 14.93vw;
  height: 6.4vw;
  letter-spacing: 0.4vw;
  text-align: center;
  line-height: 6.4vw;
  border-radius: 1.07vw;
  background: #EFEFEF;
  font-family: Source Sans Pro;
  font-size: 3.2vw;
  font-weight: 600;
  color: #999999;
}
.item-right {
  font-family: Source Han Sans CN;
  font-size: 3.73vw;
  font-weight: 500;
  color: #E20000;
}
.time-icon {
  width: 4.53vw;
  height: 4.53vw;
  margin-right: 1.07vw;
}
.item-left {
  font-family: Source Sans Pro;
  font-size: 3.73vw;
  font-weight: 600;
  line-height: 5.87vw;
  color: #666666;
}
.time {
  margin-right: 2.93vw;
  font-family: Source Sans Pro;
  font-size: 3.2vw;
  font-weight: 600;
  color: #CCCCCC;
}
.tip {
  margin-bottom: 3.2vw;
  font-family: Source Sans Pro;
  font-size: 2.67vw;
  font-weight: 600;
  line-height: 4.27vw;
  color: #CCCCCC;
}
.my-switch {
  position: relative;
  width: 10.67vw;
  height: 5.33vw;
  margin-left: 2.13vw;
  border-radius: 2.67vw;
  background: #F8F8FA;
  box-sizing: border-box;
  border: 0.27vw solid #E6E6E6;
}
.switch-circle {
  position: absolute;
  left: 0.53vw;
  top: 0.275vw;
  border-radius: 50%;
  width: 4.27vw;
  height: 4.27vw;
  background: #999;
  transition: all 0.3s linear;
}
.switch-active .switch-circle {
  left: 5.33vw;
  background: #0A67FF;
}
.add-material {
  text-align: right;
  font-family: Source Sans Pro;
  font-size: 3.73vw;
  font-weight: 600;
  line-height: 5.87vw;
  color: #006EDD;
}
.red-text {
  margin-right: 0.8vw;
  font-family: Source Sans Pro;
  font-size: 3.2vw;
  font-weight: 600;
  color: #D81E06;
}
.del-icon {
  width: 4vw;
  height: 4vw;
}
.gray-text {
  font-family: Source Sans Pro;
  font-size: 3.2vw;
  font-weight: 600;
  color: #ccc;
}
.material-list {
  box-sizing: border-box;
  padding-top: 4.27vw;
  border-top: 0.32vw solid #E6E6E6;
  padding-bottom: 5.33vw;
}
.item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: border-box;
  height: 10.67vw;
  padding-left: 2.13vw;
  font-family: Source Sans Pro;
  font-size: 3.73vw;
  font-weight: 600;
  color: #666;
  border-bottom: 0.27vw solid #E9E9E9;
}
.last-item {
  margin-bottom: 5.33vw;
  border-bottom: none;
}
.item > p {
  display: flex;
  align-items: center;
}
.green-text {
  margin-right: 2.13vw;
  font-family: Source Sans Pro;
  font-size: 3.2vw;
  font-weight: 600;
  color: #00A6A7;
}
.line {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 16vw;
  line-height: 16vw;
  box-sizing: border-box;
  padding-left: 8vw;
  padding-right: 9.33vw;
  font-family: Source Sans Pro;
  font-size: 4.8vw;
  font-weight: bold;
  line-height: 7.47vw;
  color: #333333;
}
.line > img {
  display: none;
  width: 3.47vw;
  height: 2.13vw;
}
.line.active {
  background: #E6F2FF;
  color: #0A67FF;
}
.line.active > img {
  display: block;
}
::v-deep .van-overlay {
  background: rgba(0,0,0,0.3);
}
::v-deep .van-popup {
  border-radius: 10px 10px 0 0;
}
.popup-content {
  box-sizing: border-box;
  padding-top: 5.33vw;
  height: 65.87vw;
  width: 100vw;
  background: #fff;
  border-radius: 10px 10px 0 0;
}
.right {
  display: flex;
  align-items: center;
}
.right-icon {
  width: 3vw;
  height: 3vw;
}
.cell-wrapper {
  width: 89.6vw;
  margin: 0 auto;
  box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.12);
  background: #fff;
  border-radius: 2.13vw;
  padding: 0 5.33vw 0 3.2vw;
  box-sizing: border-box;
}
.cell {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 89.6vw;
  height: 16.53vw;
  padding: 0 5.33vw 0 3.2vw;
  box-sizing: border-box;
}
.change-auth {
  padding-top: 5.33vw;
}
.label {
  font-family: Source Sans Pro;
  font-size: 3.73vw;
  font-weight: bold;
  line-height: 5.87vw;
  color: #333333;
}
</style>